<template>
	<view class="information">
		<!-- 顶部导航栏 -->
		<view class="navContent">
			<view class="nav-bar">
				<view class="back-arrow" @click="handleBack">
					<u-icon name="arrow-left" color="#000000" size="18"></u-icon>
				</view>
				<text class="nav-title">填写资料</text>
				<text class="back-arrow"></text>
			</view>
		</view>

		<view class="cartTitle">
			学生信息1
		</view>

		<view class="contentBox">
			<view class="lable">
				学校名称
			</view>
			<view class="inputBox" @click="personFlag = 1">
				<uni-combox :candidates="schoolList" placeholder="请选择学校名称"></uni-combox>
				<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode="">
				</image>
			</view>
			<view class="lable">
				年级班级
			</view>
			<view class="inputBox" style="margin-bottom: 0;" @click="handleclassShow(1)">
				<view class="inputPolicehoder">
					{{student1.className || "选择学生所属年级班级"}}
				</view>
				<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode=""></image>
			</view>

			<view class="lable" style="margin-top: 20rpx;">
				学生姓名
			</view>
			<view class="inputBox">
				<input class="input" v-model="student1.name" placeholder="输入学生姓名" />
			</view>
			<view class="lable">
				学生性别
			</view>
			<view class="inputBox" @click="handleSexShow(1)">
				<view class="inputPolicehoder">
					{{student1.sexName ||  '请选择学生性别' }}
				</view>
				<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode=""></image>
			</view>
		</view>


		<view class="cartTitle" @click="handleChangeStudent2Flag">
			<view class="">
				学生信息2
			</view>
			<image v-if="student2Flag" src="https://mp.blrwworkshop.com/imgs/lowIcon.png" mode=""></image>
			<image v-else src="https://mp.blrwworkshop.com/imgs/topIcon.png" mode=""></image>
		</view>

		<view v-if="student2Flag" class="contentBox">
			<view class="lable">
				学校名称
			</view>
			<view class="inputBox" @click="personFlag = 2">
				<uni-combox :candidates="schoolList" placeholder="请选择学校名称"></uni-combox>
				<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode="">
				</image>
			</view>
			<view class="lable">
				年级班级
			</view>
			<view class="inputBox" style="margin-bottom: 0;" @click="handleclassShow(2)">
				<view class="inputPolicehoder">
					{{student2.className || "选择学生所属年级班级"}}
				</view>
				<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode=""></image>
			</view>

			<view class="lable" style="margin-top: 20rpx;">
				学生姓名
			</view>
			<view class="inputBox">
				<input class="input" v-model="student2.name" placeholder="输入学生姓名" />
			</view>
			<view class="lable">
				学生性别
			</view>
			<view class="inputBox" @click="handleSexShow(2)">
				<view class="inputPolicehoder">
					{{student2.sexName ||  '请选择学生性别' }}
				</view>
				<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode=""></image>
			</view>
		</view>





		<view class="cartTitle">
			家长信息
		</view>
		<view class="contentBox">
			<view class="lable">
				家长姓名
			</view>
			<view class="inputBox">
				<input class="input" v-model="parentsName" placeholder="输入家长姓名" />
			</view>
			<view class="lable">
				与学生关系
			</view>
			<view class="inputBox" style="padding-left: 15rpx;" @click="identityShow = true">
				{{identityName || "请选择家长关系"}}
				<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode=""></image>
			</view>

			<view class="lable">
				家长手机号
			</view>
			<view class="inputBox" style="padding-left: 15rpx;">
				{{moblieParent}}
				<!-- <input class="input" type="number" v-model="formData.studentName" placeholder="输入家长手机号" /> -->
			</view>
		</view>

		<view class="btns_student btns" @click="handleSave">
			提交
		</view>

		<!-- 班级年级 -->
		<u-picker :show="classShow" ref="uPicker" confirmColor="#FFA116" :columns="classList"
			@cancel="classShow = false" @change="changeClassList" @confirm="confirmClassList"></u-picker>

		<!-- 性别 -->
		<u-picker :show="sexshow" keyName="name" confirmColor="#FFA116" :columns="sexList" @cancel="sexshow = false"
			@confirm="confirmSex"></u-picker>

		<!-- 关系 -->
		<u-picker :show="identityShow" keyName="name" confirmColor="#FFA116" :columns="ParentList"
			@cancel="identityShow = false" @confirm="confirmParent"></u-picker>


	</view>
</template>

<script>
	import {
		listAllSchool,
		qryGradeClazzTree,
		getParentsInfo,
		bindStudent
	} from "@/api/api.js"
	export default {
		data() {
			return {
				parentsName: "",
				moblieParent: "",
				identityType: "",
				identityName: "",
				identityShow: false,
				ParentList: [
					[{
						name: "爸爸",
						id: 1
					}, {
						name: "妈妈",
						id: 2
					}]
				],
				student1: {
					schoolId: "",
					clazzId: "",
					className: "",
					name: "",
					sex: null,
					sexName: "",
				},
				student2: {
					schoolId: "",
					clazzId: "",
					className: "",
					name: "",
					sex: null,
					sexName: "",
				},
				schoolList: [],
				classShow: false,
				classData: [],
				classList: [
					[],
					[]
				],
				sexshow: false,
				sexList: [
					[{
						name: "男",
						id: 0
					}, {
						name: "女",
						id: 1
					}, ]
				],
				personFlag: 1, //1 学生1 2 学生2
				student2Flag: false,


			}
		},
		onLoad() {
			this.handleGetlistAll()
			this.handleGetParentsInfo()
			let that = this
			uni.$on("changeSchool", (data) => {
				if (that.personFlag == 1) {
					that.student1.schoolId = data.schoolId
					that.student1.clazzId = ""
					that.student1.className = ""
					that.handleqryGradeClazzTree()
				} else {
					that.student2.schoolId = data.schoolId
					that.student2.clazzId = ""
					that.student2.className = ""
					that.handleqryGradeClazzTree()
				}

			})
		},
		methods: {
			//获取家长信息
			async handleGetParentsInfo() {
				let res = await getParentsInfo()
				if (res.code == 200) {
					this.moblieParent = res.parentsPhone
				}
			},
			//获取学校列表
			async handleGetlistAll() {
				let res = await listAllSchool()
				if (res.code == 200) {
					this.schoolList = res.data
				}
			},
			handleclassShow(type) {

				if (this.student1.schoolId == "" && type == 1) {
					uni.showToast({
						title: '请先选择学生1的学校',
						icon: 'none'
					})
					return
				}
				if (this.student2.schoolId == "" && type == 2) {
					uni.showToast({
						title: '请先选择学生2的学校',
						icon: 'none'
					})
					return
				}
				this.personFlag = type
				this.classShow = true
			},
			//获取年假班级树形数据
			async handleqryGradeClazzTree() {
				let res = await qryGradeClazzTree({
					schoolId: this.personFlag == 1 ? this.student1.schoolId : this.student2.schoolId,
				})
				if (res.code == 200) {
					this.classData = res.data
					this.classList = [
						[],
						[]
					]
					// console.log("年级班级", this.classData[0].clazzList)
					res.data.forEach((item, index) => {
						this.classList[0].push(item.gradeName)
					})
					this.classData[0].clazzList.forEach((item, index) => {
						this.classList[1].push(item.clazzName)
					})
				}
			},

			confirmClassList(e) {
				if (this.personFlag == 1) {
					this.student1.className = e.value.join('/')
					this.student1.clazzId = this.classData[e.indexs[0]].clazzList[e.indexs[1]].clazzId
				}
				if (this.personFlag == 2) {
					this.student2.className = e.value.join('/')
					this.student2.clazzId = this.classData[e.indexs[0]].clazzList[e.indexs[1]].clazzId
				}
				this.classShow = false
			},
			changeClassList(e) {
				const {
					columnIndex,
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e

				if (columnIndex == 0) {
					this.classList[1] = []
					this.classData[index].clazzList.forEach((item, index) => {
						this.classList[1].push(item.clazzName)
					})
					this.$forceUpdate()
					// console.log("classList", this.classList[1])
				}
			},

			handleSexShow(value) {
				this.personFlag = value
				this.sexshow = true
			},
			confirmSex(e) {
				if (this.personFlag == 1) {
					this.student1.sex = e.indexs[0]
					this.student1.sexName = e.value[0].name
				}
				if (this.personFlag == 2) {
					this.student2.sex = e.indexs[0]
					this.student2.sexName = e.value[0].name
				}
				this.sexshow = false
			},
			confirmParent(e) {
				this.identityType = e.value[0].id
				this.identityName = e.value[0].name
				this.identityShow = false
				// console.log("e.indexs[0].id", e.indexs[0].id)
			},
			handleChangeStudent2Flag() {
				this.student2Flag = !this.student2Flag
				this.student2 = {
					schoolId: "",
					clazzId: "",
					className: "",
					name: "",
					sex: null,
					sexName: "",
				}
			},
			async handleSave() {
				if (this.student1.schoolId == "") {
					uni.showToast({
						title: '请先选择学生1的学校',
						icon: 'none'
					})
					return
				}
				if (this.student1.clazzId == "") {
					uni.showToast({
						title: '请先选择学生1的所属年级班级',
						icon: 'none'
					})
					return
				}
				if (this.student1.name == "") {
					uni.showToast({
						title: '请先输入学生1的姓名',
						icon: 'none'
					})
					return
				}
				if (this.student1.sex == null) {
					uni.showToast({
						title: '请先选择学生1的性别',
						icon: 'none'
					})
					return
				}
				if (this.student2Flag) {
					if (this.student2.schoolId == "") {
						uni.showToast({
							title: '请先选择学生2的学校',
							icon: 'none'
						})
						return
					}
					if (this.student2.clazzId == "") {
						uni.showToast({
							title: '请先选择学生2的所属年级班级',
							icon: 'none'
						})
						return
					}
					if (this.student2.name == "") {
						uni.showToast({
							title: '请先输入学生2的姓名',
							icon: 'none'
						})
						return
					}
					if (this.student2.sex == null) {
						uni.showToast({
							title: '请先选择学生2的性别',
							icon: 'none'
						})
						return
					}
				}
				if (this.parentsName == "") {
					uni.showToast({
						title: '请先输入家长姓名',
						icon: 'none'
					})
					return
				}
				if (this.identityType == "") {
					uni.showToast({
						title: '请先选择家长关系',
						icon: 'none'
					})
					return
				}
				let data = {
					studentsDetailsDtoList: [{
						schoolId: this.student1.schoolId,
						clazzId: this.student1.clazzId,
						name: this.student1.name,
						sex: this.student1.sex,
					}],
					parentsName: this.parentsName,
					identityType: this.identityType
				}
				if (this.student2Flag) {
					data.studentsDetailsDtoList[1] = {
						schoolId: this.student2.schoolId,
						clazzId: this.student2.clazzId,
						name: this.student2.name,
						sex: this.student2.sex,
					}
				}
				// console.log("data", data)
				let res = await bindStudent(data)
				if (res.code == 200) {
					uni.showToast({
						title: '提交成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.redirectTo({
							url: "/pages/login/character"
						})
					}, 1500)
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			handleBack() {
				uni.navigateBack()
			},
		}
	}
</script>

<style scoped lang="scss">
	.information {
		width: 100%;
		min-height: 100vh;
		background: linear-gradient(178deg, #FFE6A0 0%, rgba(255, 162, 22, 0) 100%);
		padding-bottom: 120rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.navContent {
			.nav-bar {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 88rpx 32rpx 24rpx 32rpx;
				font-size: 34rpx;
				color: #000000;

				.nav-title {
					width: 100%;
					text-align: center;
				}
			}
		}

		.cartTitle {
			font-weight: 600;
			font-size: 36rpx;
			color: #333333;
			line-height: 50rpx;
			width: 100%;
			padding: 0 48rpx;
			box-sizing: border-box;
			margin-top: 66rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			image {
				width: 48rpx;
				height: 48rpx;
			}
		}

		.contentBox {
			width: 690rpx;
			// height: 760rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(255, 192, 143, 0.19);
			border-radius: 20rpx;
			margin-top: 16rpx;
			padding: 52rpx 30rpx;
			box-sizing: border-box;

			.lable {
				font-weight: 400;
				font-size: 32rpx;
				color: #333333;
				line-height: 44rpx;
				margin-bottom: 16rpx;
			}

			.inputBox {
				width: 626rpx;
				height: 80rpx;
				border-radius: 10rpx;
				border: 2rpx solid #F3F3F3;
				margin-bottom: 25rpx;
				position: relative;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 28rpx;

				.inputPolicehoder {
					padding: 0 20rpx;
					box-sizing: border-box;
					font-size: 28rpx;
					color: gray;
				}

				.input {
					width: 100%;
					height: 60%;
					padding-left: 20rpx;
					font-size: 28rpx;
				}

				image {
					width: 44rpx;
					height: 44rpx;
				}

				.dropdown {
					position: absolute;
					left: 0;
					right: 0;
					top: 80rpx;
					background: #fff;
					border: 1rpx solid #eee;
					border-radius: 0 0 10rpx 10rpx;
					z-index: 99;
					max-height: 300rpx;
					overflow-y: auto;
					box-shadow: 0 4rpx 16rpx 0 rgba(0, 0, 0, 0.08);
					padding-bottom: 20rpx;

					.dropdown-item {
						padding: 20rpx 24rpx;
						font-size: 28rpx;
						color: #333;
						// border-bottom: 1rpx solid #f3f3f3;
					}

					.dropdown-item:last-child {
						border-bottom: none;
					}
				}
			}
		}

		.btns {
			position: fixed;
			bottom: 80rpx;
			z-index: 999;
		}
	}
</style>